module TagsHelper
  # 通过 id和type获取对象
  def get_object(obj_id,obj_type)
    @obj = nil
    case obj_type
    when '1'
      @obj = User.find_by_id(obj_id)
    when '2'
      @obj = Project.find_by_id(obj_id)
    when '3'
      @obj = Issue.find_by_id(obj_id)
    when '4'
      @obj = Bid.find_by_id(obj_id)
    when '6'
    when '7'
      @obj= Contest.find_by_id(obj_id)
    else
      raise Exception, '[TagsHelper] ===> tag type unknow.'
    end
    return @obj
  end
    
  # 判断登录用户是否可以对用户、项目、需求、问题中tag进行删除动作
  # 这里关于项目可以使用User.memeber_of?来做判断，这样可以提高该函数效率，但对满足需求有偏差
  # 返回值为true 或  false
  def can_remove_tag(user,obj_id,obj_type)
    @result = false
    case obj_type
    when '1'    # 对用户 是否是本人
       @obj = User.find_by_id(obj_id)
       if user == @obj
         @result = true
       end
    when '2'
      @result = is_manager?(user.id,obj_id)
    when '3'
      @project_id = Issue.find_by_id(obj_id)
      @result = is_manager?(user.id,@project_id)
    when '4'
      if user.id == obj_id
        @result = true
      end
    when '7'
      if user.id == obj_id
        @result = true
      end
    end
    return @result
  end
  
end